package info

import (
	"encoding/json"
	"time"

	"github.com/yusufpapurcu/wmi"
)

type Win32_Battery struct {
	Availability                uint16
	BatteryRechargeTime         uint32
	BatteryStatus               uint16
	Caption                     string
	Chemistry                   uint16
	ConfigManagerErrorCode      uint32
	ConfigManagerUserConfig     bool
	CreationClassName           string
	Description                 string
	DesignCapacity              uint32
	DesignVoltage               uint64
	DeviceID                    string
	ErrorCleared                bool
	ErrorDescription            string
	EstimatedChargeRemaining    uint16
	EstimatedRunTime            uint32
	ExpectedBatteryLife         uint32
	ExpectedLife                uint32
	FullChargeCapacity          uint32
	InstallDate                 time.Time
	LastErrorCode               uint32
	MaxRechargeTime             uint32
	Name                        string
	PNPDeviceID                 string
	PowerManagementCapabilities []uint16
	PowerManagementSupported    bool
	SmartBatteryVersion         string
	Status                      string
	StatusInfo                  uint16
	SystemCreationClassName     string
	SystemName                  string
	TimeOnBattery               uint32
	TimeToFullCharge            uint32
}

func BatteryInfo() (battery []Win32_Battery, err error) {
	err1 := wmi.Query("Select * from Win32_Battery", &battery)
	if err1 != nil {
		return nil, err1
	}

	return battery, nil
}

func JsonBatteryInfo() (t []byte, err error) {
	battery, err := BatteryInfo()
	if err != nil {
		return t, err
	}
	t, jsonerr := json.Marshal(battery)
	if jsonerr != nil {
		return t, jsonerr
	}
	return t, nil
}

func MapJsonBatteryInfo() (data []map[string]interface{}, err error) {

	battery, err := JsonBatteryInfo()
	if err != nil {
		return data, err
	}
	json.Unmarshal(battery, &data)
	return data, nil
}
